package com.hurui.xiaoming.activity.main;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.hurui.xiaoming.R;
import com.hurui.xiaoming.activity.BaseActivity;
import com.hurui.xiaoming.activity.login.LoginActivity;
import com.hurui.xiaoming.bean.Li_IndexUI_Result;
import com.hurui.xiaoming.myconfig.PamaryKeys;
import com.hurui.xiaoming.utils.DefultData;
import com.hurui.xiaoming.utils.WriteReadSharedPrefs;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends BaseActivity {
    private int last_tab = 0 ;

    FragmentTabHost tabHost;
    int index=0;
    Map map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        index=getIntent().getIntExtra(PamaryKeys.UI_ID,0);
        map = new HashMap();
        initView();
        initTab();
        initAction();

        //isOverdue();
    }





    @Override
    public void initView() {
        tabHost= (FragmentTabHost) findViewById(android.R.id.tabhost);
        tabHost.setup(this,getSupportFragmentManager(),R.id.content);
    }

    private void initTab() {
        MainTab[] tabs = MainTab.values();
        final int size = tabs.length;
        for (int i=0;i<size ;i++){
            MainTab mainTab=tabs[i];
            TabHost.TabSpec tab=tabHost.newTabSpec(getString(mainTab.getResName()));
            View indication= LayoutInflater.from(this).inflate(R.layout.tab_indication,null);
            LinearLayout lin = (LinearLayout) indication.findViewById(R.id.lin);
            TextView textView= (TextView) indication.findViewById(R.id.tab_title);
            textView.setText(mainTab.getResName());
            TextView textView_nub = (TextView) indication.findViewById(R.id.tab_nub);
            ImageView imageView = (ImageView) indication.findViewById(R.id.tab_icon);
            //textView.setTextColor(getResources().getColor(R.color.clodGray));
            Drawable drawable= ContextCompat.getDrawable(this,mainTab.getResIcon());
            imageView.setImageDrawable(drawable);
            tab.setIndicator(indication);
            tabHost.addTab(tab,mainTab.getClz(),null);
            tabHost.getTabWidget().setDividerDrawable(null);//去掉竖直分割线

        }
        tabHost.setCurrentTab(index);
    }

    @Override
    public void initAction() {
        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                Log.d("打印出来的id为:" , tabId);
                if(tabId.equals("更多")){
                    onToast("该功能尚未开通");
                    tabHost.setCurrentTab(last_tab);
                }else{
                    last_tab = tabHost.getCurrentTab() ;
                }
            }
        });
    }

    /**
     * 判断用户信息是否过期
     */
    public void isOverdue(){
        map.put("ui_type",WriteReadSharedPrefs.readUserUI_Type(mContext));
        map.put("ui_id", String.valueOf(WriteReadSharedPrefs.readUserID(mContext)));
        Log.i("mapmap",map.toString());
        Call<Li_IndexUI_Result> call = apiSevice.onHttpPostIndex(DefultData.indexData,map);
        call.enqueue(new Callback<Li_IndexUI_Result>() {
            @Override
            public void onResponse(Call<Li_IndexUI_Result> call, Response<Li_IndexUI_Result> response) {
                if(response.isSuccessful()){
                    Li_IndexUI_Result li_indexUI_result = response.body();
                    if(isStop(Integer.parseInt(li_indexUI_result.getData().getUi_lastlogintime()))){
                        onOpen(new Intent(mContext , LoginActivity.class));
                        onFinish();
                    }
                }else{
                    onToast("失败");
                }
            }

            @Override
            public void onFailure(Call<Li_IndexUI_Result> call, Throwable t) {
                onHttpFail( -1 , t.getMessage());
            }
        });
    }
}
